// 月份选择
const componentBehaviors = require('../../../behaviors/component');
import { COMPONENT_OPTIONS } from '../../../constants/common';
import dayjs from 'dayjs';
import { createMonthColumns } from '../../../biztools/common';
const { formatDate } = require('../../../common/utils');
const now = dayjs();
const formatTemplate = 'YYYY年MM月';
const monthData = createMonthColumns(2020, 1);

Component({
    behaviors: [componentBehaviors],
    options: {
        ...COMPONENT_OPTIONS,
    },
    properties: {
        defaultValue: {
            type: Object,
            value: now,
            observer: function (newVal, oldVal) {
                this.setData({
                    monthText: formatDate(newVal, formatTemplate),
                });
            },
        },
        // 收入
        income: {
            type: Number,
            value: 0,
        },
        // 支出
        expend: {
            type: Number,
            value: 0,
        },
    },
    data: {
        monthValue: now,
        monthText: now.format(formatTemplate),
        showSelect: false,
        monthColumns: [{
            values: Object.keys(monthData),
            className: 'column1',
        }, {
            values: monthData[now.year()],
            className: 'column2',
        }],
    },
    methods: {
        // 打开选择
        openSelectModal: function () {
            this.setData({
                showSelect: true,
            });
        },
        closeSelectModal() {
            this.setData({
                showSelect: false,
            });
        },
        // 选择数据变化
        onValueChange: function (event) {
            const { picker, value } = event.detail;
            picker.setColumnValues(1, monthData[value[0]]);
        },
        ensureChangeMonth: function (event) {
            const { value } = event.detail;
            this.setData({
                showSelect: false,
                monthText: dayjs(new Date(value[0], value[1] - 1, 1)).format(formatTemplate),
            });
            this.triggerEvent('change', {
                year: parseInt(value[0]),
                month: parseInt(value[1]),
            });
        },
    }
})
